package ru.kop4en.diplom;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.headless.HeadlessMediaPlayer;

/**
 * An example of how to stream a media file over HTTP.
 * <p>
 * The client specifies an MRL of <code>http://127.0.0.1:5555</code>
 */
public class StreamTest
{

	public static void main(String[] args) throws Exception
	{

		NativeLibrary.addSearchPath("libvlc", "C:\\Program Files\\VideoLAN\\VLC");

		String media = "dshow://";
		String[] options = formatHttpStream("192.168.1.2", 5555);

		System.out.println("Streaming '" + media + "' to '" + options + "'");
	
		MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory("");
		HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory
				.newHeadlessMediaPlayer();
		mediaPlayer.playMedia(media, options);
		//"dshow-vdev=USB Video Device #1"

		// Don't exit
		Thread.currentThread().join();
	}
	
	static void print(String...strings)
	{
		for (String str : strings)
		{
			System.out.println(str);			
		}
	}

	private static String[] formatHttpStream(String serverAddress, int serverPort)
	{

		File dir = new File(System.getProperty("user.home"), "Videos");
		dir.mkdirs();

		DateFormat df = new SimpleDateFormat("dd-MM-yyyy-HH.mm:ss");
		String fileName = dir.getAbsolutePath() + "/Capture-"
				+ df.format(new Date()) + ".mpg";

		String[] options = new String[2];
		
		StringBuilder sb = new StringBuilder(60);

		// this is work
		// sb.append(":sout=#transcode{vcodec=h264,acodec=mp3}:duplicate{dst=file{dst="
		// + fileName + "},dst=std{access=http,mux=asf,");

		options[0] = ":dshow-vdev=USB Video Device #1";
		options[1] = ":sout=#transcode{vcodec=mpgv,acodec=mpga}:duplicate{dst=file{dst="+ fileName + "},dst=display}";
		//sb.append(":sout=#transcode{vcodec=mpgv,acodec=mpga}:duplicate{dst=file{dst="+ fileName + "},dst=display}");//,dst=std{access=http,mux=asf,");				
//		sb.append("dst=");
//		sb.append(serverAddress);
//		sb.append(':');
//		sb.append(serverPort);
//		sb.append("/out}}");
		return options;
	}
}

